package mbp.gui.base;

import java.awt.ScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JMenuItem;

/**
 * Tabela z lista zasobow wraz z dodatkowymi informacjami. Obiekt zaprojektowany
 * do wykorzystania przy panelu edycyjnym kontroferty.
 * @author wojtekw
 *
 */
public class ResourceListScrollPane extends JScrollPane
{
	private JTable RcTable;
	private DefaultTableModel RcModel;
	
	public ResourceListScrollPane()
	{
		//Utworzenie tabeli z wierszami tylko do odczytu
		RcTable = new JTable(){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		  };
		RcTable.setToolTipText("Double click for Resource details, Right click for options");
		  
		
		RcTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		RcTable.setFillsViewportHeight(true);
		this.setViewportView(RcTable);
		
		RcTable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"i", "Type", "Term", "Resource"
				}
			));
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(RcTable, popupMenu);
		
		JMenuItem mntmChangeTermItem = new JMenuItem("Change term");
		mntmChangeTermItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				changeTerm();
			}
		});		
		popupMenu.add(mntmChangeTermItem);
		
		JMenuItem mntmAddResource = new JMenuItem("Add Resource");
		mntmAddResource.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				addResource();
			}
		});
		popupMenu.add(mntmAddResource);
		
		JMenuItem mntmRemoveResource = new JMenuItem("Remove resource");
		mntmRemoveResource.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				removeResource();
			}
		});
		popupMenu.add(mntmRemoveResource);
		
		RcModel = (DefaultTableModel) RcTable.getModel();
		
		//podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		RcTable.addMouseListener(new MouseAdapter() {
		     public void mouseClicked(MouseEvent e) {
		      if (e.getClickCount() == 2) {
		    	  processDoubleClick();
		         }
		      }
		     });
		RcTable.getColumnModel().getColumn(0).setMinWidth(20);
		RcTable.getColumnModel().getColumn(0).setMaxWidth(20);
	}
	
	/**
	 * Metody przeznaczone do nadpisania (zaprogramowania) przy tworzeniu obiektu
	 * zaleznie od spospbu wykorzystania obiektu
	 */
	//-------------------------------------------------------------------------------
	public void processDoubleClick()
	{
		System.out.println("processDoubleClick()");
	}
	
	public void addResource(){}
	
	public void removeResource(){}
	
	public void changeTerm(){}
	//-------------------------------------------------------------------------------
	
	
	/**
	 * Zwraca model tabeli. Umozliwia dostep do tabeli
	 * @return
	 */
	public DefaultTableModel getTableModel()
	{
		return RcModel;
	}
	
	/**
	 * Zwraca aktualnie zaznaczony wiersz
	 * @return
	 */
	public int getSelectedRow()
	{
		return RcTable.getSelectedRow();
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
